home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 688 / updateicon / updateicon.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  13KB  |  501 lines

  1. /*
  2. **    UpdateIcon - Icon control utility
  3. **
  4. **    © Copyright 1992 by Olaf `Olsen' Barthel
  5. **        All Rights Reserved
  6. **
  7. **    Public-domain, both commercial and noncommercial use
  8. **    of this program are permitted.
  9. */
  10.  
  11.     /* System includes. */
  12.  
  13. #include <workbench/workbench.h>
  14. #include <exec/execbase.h>
  15. #include <dos/dosextens.h>
  16. #include <exec/memory.h>
  17. #include <dos/dosasl.h>
  18.  
  19.     /* Library prototypes. */
  20.  
  21. #include <clib/utility_protos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/icon_protos.h>
  24. #include <clib/dos_protos.h>
  25.  
  26.     /* Argument vector offsets. */
  27.  
  28. enum    {    ARG_NAME,ARG_ADD,ARG_UPDATE,ARG_REPLACE,ARG_DEFAULTOOL,ARGCOUNT };
  29.  
  30.     /* Version tag identifier. */
  31.  
  32. STATIC UBYTE *Version = "\0$VER: UpdateIcon 1.0 (18.4.92)";
  33.  
  34.     /* Shared library identifiers. */
  35.  
  36. struct ExecBase        *SysBase;
  37. struct DosLibrary    *DOSBase;
  38. struct Library        *IconBase;
  39. struct Library        *UtilityBase;
  40.  
  41.     /* Main():
  42.      *
  43.      *    The one and only main routine.
  44.      */
  45.  
  46. LONG __saveds
  47. Main()
  48. {
  49.     struct Process    *ThisProcess;
  50.     LONG         Result = RETURN_FAIL;
  51.  
  52.         /* Set up ExecBase pointer. */
  53.  
  54.     SysBase = *(struct ExecBase **)4;
  55.  
  56.         /* Obtain current process identifier. */
  57.  
  58.     ThisProcess = (struct Process *)SysBase -> ThisTask;
  59.  
  60.         /* Did we get started from Shell? */
  61.  
  62.     if(ThisProcess -> pr_CLI)
  63.     {
  64.             /* Open dos.library */
  65.  
  66.         if(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",37))
  67.         {
  68.                 /* Open icon.library */
  69.  
  70.             if(IconBase = OpenLibrary("icon.library",37))
  71.             {
  72.                     /* Open utility.library */
  73.  
  74.                 if(UtilityBase = OpenLibrary("utility.library",37))
  75.                 {
  76.                     struct AnchorPath *Anchor;
  77.  
  78.                         /* Allocate pattern matching data. */
  79.  
  80.                     if(Anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath) + 256,MEMF_ANY | MEMF_CLEAR))
  81.                     {
  82.                         STRPTR *Args;
  83.  
  84.                             /* Allocate argument vector. */
  85.  
  86.                         if(Args = (STRPTR)AllocVec(sizeof(STRPTR) * ARGCOUNT,MEMF_ANY | MEMF_CLEAR))
  87.                         {
  88.                             struct RDArgs *ArgsPtr;
  89.  
  90.                                 /* Allocate argument parser data. */
  91.  
  92.                             if(ArgsPtr = (struct RDArgs *)AllocDosObjectTags(DOS_RDARGS,TAG_DONE))
  93.                             {
  94.                                     /* Provide extra help infocmation. */
  95.  
  96.                                 ArgsPtr -> RDA_ExtHelp = "\nUsage: UpdateIcon <Name pattern> [Add] [Update]\n\
  97.                   [Replace <Tool name to replace>] [DefaultTool <Name>]\n\n\
  98.                           `Add' adds  an  icon  to a file which does not yet\n\
  99.                                 have an icon attached.\n\n\
  100.                        `Update' updates the position of an icon.\n\n\
  101.                       `Replace' replaces the default tool name of an icon by\n\
  102.                                 the name specified using `DefaultTool'.\n\n\
  103.                   `DefaultTool' can be used in conjunction with `Replace' or\n\
  104.                                 alone  by  itself.   In the latter case, any\n\
  105.                                 icon  in question will have the default tool\n\
  106.                                 set to this name.\n";
  107.  
  108.                                     /* Allow the pattern matching process to
  109.                                      * be aborted by pressing ^C.
  110.                                      */
  111.     
  112.                                 Anchor -> ap_BreakBits    = SIGBREAKF_CTRL_C;
  113.                                 Anchor -> ap_Strlen    = 256;
  114.     
  115.                                     /* Read the command arguments. */
  116.     
  117.                                 if(ReadArgs("NAME,A=ADD/S,U=UPDATE/S,R=REPLACE/K,T=DEFAULTTOOL/K",(LONG *)Args,ArgsPtr))
  118.                                 {
  119.                                         /* Did we get a name pattern? */
  120.     
  121.                                     if(Args[ARG_NAME])
  122.                                     {
  123.                                         struct DiskObject    *Icon        = NULL;
  124.                                         BYTE             Continue    = TRUE,
  125.                                                      Match        = FALSE;
  126.                                         WORD             Len;
  127.     
  128.                                             /* Look for the first matching file name. */
  129.     
  130.                                         if(!MatchFirst(Args[ARG_NAME],Anchor))
  131.                                         {
  132.                                             STRPTR Name = Anchor -> ap_Buf;
  133.     
  134.                                                 /* So far, we have been pretty successful. */
  135.     
  136.                                             Result    = RETURN_OK;
  137.     
  138.                                                 /* At least a single match has been made. */
  139.     
  140.                                             Match    = TRUE;
  141.     
  142.                                                 /* Scan until no matches remain. */
  143.     
  144.                                             for(;;)
  145.                                             {
  146.                                                 Len = strlen(Name);
  147.     
  148.                                                     /* Is the name likely to
  149.                                                      * have `.info' appended?
  150.                                                      */
  151.     
  152.                                                 if(Len > 5)
  153.                                                 {
  154.                                                         /* Check for `.info' suffix,
  155.                                                          * we don't wish to attach
  156.                                                          * icons to icon files.
  157.                                                          */
  158.     
  159.                                                     if(!Stricmp(&Name[Len - 5],".info"))
  160.                                                     {
  161.                                                             /* Look for the next matching file. */
  162.     
  163.                                                         if(MatchNext(Anchor))
  164.                                                         {
  165.                                                             LONG Error = IoErr();
  166.     
  167.                                                             if(Error != ERROR_NO_MORE_ENTRIES)
  168.                                                                 PrintFault(Error,"UpdateIcon");
  169.     
  170.                                                             break;
  171.                                                         }
  172.     
  173.                                                         continue;
  174.                                                     }
  175.                                                 }
  176.     
  177.                                                     /* Did we get a proper name? */
  178.     
  179.                                                 if(Name[0])
  180.                                                 {
  181.                                                         /* Are we to add icons to files
  182.                                                          * which do not yet have an icon
  183.                                                          * attached?
  184.                                                          */
  185.     
  186.                                                     if(Args[ARG_ADD])
  187.                                                     {
  188.                                                             /* Try to read the icon file if any. */
  189.     
  190.                                                         if(!(Icon = GetDiskObject(Name)))
  191.                                                         {
  192.                                                                 /* No icon file present,
  193.                                                                  * try to obtain a default
  194.                                                                  * icon image.
  195.                                                                  */
  196.     
  197.                                                             if(Icon = GetDiskObjectNew(Name))
  198.                                                             {
  199.                                                                     /* No fixed position for the
  200.                                                                      * new icon, please.
  201.                                                                      */
  202.     
  203.                                                                 Icon -> do_CurrentX = NO_ICON_POSITION;
  204.                                                                 Icon -> do_CurrentY = NO_ICON_POSITION;
  205.     
  206.                                                                     /* The icon file will be saved only
  207.                                                                      * if there are no other actions
  208.                                                                      * to execute using the icon data.
  209.                                                                      */
  210.     
  211.                                                                 if(!Args[ARG_UPDATE] && !Args[ARG_REPLACE] && !Args[ARG_DEFAULTOOL])
  212.                                                                 {
  213.                                                                         /* Try to save the new icon
  214.                                                                          * to disk...
  215.                                                                          */
  216.     
  217.                                                                     if(!PutDiskObject(Name,Icon))
  218.                                                                     {
  219.                                                                         Printf("UpdateIcon: Could not save icon for \"%s\"!\a\n",Name);
  220.     
  221.                                                                         Continue = FALSE;
  222.                                                                     }
  223.     
  224.                                                                         /* Free the icon data. */
  225.     
  226.                                                                     FreeDiskObject(Icon);
  227.     
  228.                                                                     Icon = NULL;
  229.                                                                 }
  230.                                                             }
  231.                                                             else
  232.                                                             {
  233.                                                                 Printf("UpdateIcon: Could not get icon for \"%s\"!\a\n",Name);
  234.     
  235.                                                                 Continue = FALSE;
  236.                                                             }
  237.                                                         }
  238.                                                         else
  239.                                                         {
  240.                                                                 /* The file already has a proper icon
  241.                                                                  * attached. If not to be used later,
  242.                                                                  * free it.
  243.                                                                  */
  244.     
  245.                                                             if(!Args[ARG_UPDATE] && !Args[ARG_REPLACE] && !Args[ARG_DEFAULTOOL])
  246.                                                             {
  247.                                                                 FreeDiskObject(Icon);
  248.     
  249.                                                                 Icon = NULL;
  250.                                                             }
  251.                                                         }
  252.                                                     }
  253.     
  254.                                                         /* Are we to update the icon
  255.                                                          * position?
  256.                                                          */
  257.     
  258.                                                     if(Args[ARG_UPDATE] && Continue)
  259.                                                     {
  260.                                                             /* Do we already have an
  261.                                                              * icon to work upon?
  262.                                                              */
  263.     
  264.                                                         if(!Icon)
  265.                                                         {
  266.                                                                 /* Try to read the icon. */
  267.     
  268.                                                             if(!(Icon = GetDiskObject(Name)))
  269.                                                             {
  270.                                                                 Printf("UpdateIcon: Could not get icon for \"%s\"!\a\n",Name);
  271.     
  272.                                                                 Continue = FALSE;
  273.                                                             }
  274.                                                         }
  275.     
  276.                                                             /* Reset the icon position. */
  277.     
  278.                                                         if(Icon)
  279.                                                         {
  280.                                                             Icon -> do_CurrentX = NO_ICON_POSITION;
  281.                                                             Icon -> do_CurrentY = NO_ICON_POSITION;
  282.                                                         }
  283.                                                     }
  284.     
  285.                                                         /* Are we to replace the default
  286.                                                          * tool of an icon with a different
  287.                                                          * tool name?
  288.                                                          */
  289.     
  290.                                                     if(Args[ARG_REPLACE] && !Continue)
  291.                                                     {
  292.                                                             /* Did the user give a
  293.                                                              * proper replacement
  294.                                                              * tool name?
  295.                                                              */
  296.     
  297.                                                         if(!Args[ARG_DEFAULTOOL])
  298.                                                         {
  299.                                                             PrintFault(ERROR_REQUIRED_ARG_MISSING,"UpdateIcon");
  300.     
  301.                                                             Result = RETURN_ERROR;
  302.     
  303.                                                             Continue = FALSE;
  304.                                                         }
  305.                                                         else
  306.                                                         {
  307.                                                                 /* Do we already have an
  308.                                                                  * icon to play with?
  309.                                                                  */
  310.     
  311.                                                             if(!Icon)
  312.                                                             {
  313.                                                                 if(!(Icon = GetDiskObject(Name)))
  314.                                                                 {
  315.                                                                     Printf("UpdateIcon: Could not get icon for \"%s\"!\a\n",Name);
  316.     
  317.                                                                     Continue = FALSE;
  318.                                                                 }
  319.                                                             }
  320.     
  321.                                                             if(Icon)
  322.                                                             {
  323.                                                                     /* Does this icon have
  324.                                                                      * a default tool?
  325.                                                                      */
  326.     
  327.                                                                 if(Icon -> do_DefaultTool)
  328.                                                                 {
  329.                                                                         /* If the tool name matches the one
  330.                                                                          * we are to replace, replace it
  331.                                                                          * with the new name.
  332.                                                                          */
  333.     
  334.                                                                     if(!Stricmp(Icon -> do_DefaultTool,Args[ARG_REPLACE]))
  335.                                                                         Icon -> do_DefaultTool = (char *)Args[ARG_DEFAULTOOL];
  336.                                                                 }
  337.                                                             }
  338.                                                         }
  339.                                                     }
  340.                                                     else
  341.                                                     {
  342.                                                             /* Are we to supply a new default tool
  343.                                                              * for an existing icon?
  344.                                                              */
  345.     
  346.                                                         if(Args[ARG_DEFAULTOOL] && Continue)
  347.                                                         {
  348.                                                                 /* Do we already have an
  349.                                                                  * icon to work upon?
  350.                                                                  */
  351.     
  352.                                                             if(!Icon)
  353.                                                             {
  354.                                                                 if(!(Icon = GetDiskObject(Name)))
  355.                                                                 {
  356.                                                                     Printf("UpdateIcon: Could not get icon for \"%s\"!\a\n",Name);
  357.     
  358.                                                                     Continue = FALSE;
  359.                                                                 }
  360.                                                             }
  361.     
  362.                                                                 /* Supply the new default tool name. */
  363.     
  364.                                                             if(Icon)
  365.                                                                 Icon -> do_DefaultTool = (char *)Args[ARG_DEFAULTOOL];
  366.                                                         }
  367.                                                     }
  368.     
  369.                                                         /* That's all folks:
  370.                                                          * save the icon back to
  371.                                                          * disk.
  372.                                                          */
  373.     
  374.                                                     if(Icon)
  375.                                                     {
  376.                                                             /* Try to save the icon... */
  377.     
  378.                                                         if(!PutDiskObject(Name,Icon))
  379.                                                         {
  380.                                                             Printf("UpdateIcon: Could not save icon for \"%s\"!\a\n",Name);
  381.     
  382.                                                             Continue = FALSE;
  383.                                                         }
  384.                                                         else
  385.                                                             Result = RETURN_OK;
  386.                                                                                                                 
  387.                                                             /* Release the icon data. */
  388.     
  389.                                                         FreeDiskObject(Icon);
  390.     
  391.                                                         Icon = NULL;
  392.                                                     }
  393.                                                                                                      
  394.                                                         /* Did we get an error? */
  395.     
  396.                                                     if(!Continue)
  397.                                                     {
  398.                                                         Result = RETURN_FAIL;
  399.     
  400.                                                         break;
  401.                                                     }
  402.                                                 }
  403.     
  404.                                                     /* Look for the next matching name. */
  405.     
  406.                                                 if(MatchNext(Anchor))
  407.                                                 {
  408.                                                     LONG Error = IoErr();
  409.     
  410.                                                     if(Error != ERROR_NO_MORE_ENTRIES)
  411.                                                         PrintFault(Error,"UpdateIcon");
  412.     
  413.                                                     break;
  414.                                                 }
  415.                                             }
  416.                                         }
  417.     
  418.                                             /* If no match has been
  419.                                              * made, tell it to the
  420.                                              * user.
  421.                                              */
  422.     
  423.                                         if(!Match)
  424.                                         {
  425.                                             Printf("UpdateIcon: No applicable objects.\n");
  426.     
  427.                                             Result = RETURN_WARN;
  428.                                         }
  429.     
  430.                                             /* Free pattern matching info. */
  431.     
  432.                                         MatchEnd(Anchor);
  433.                                     }
  434.                                     else
  435.                                     {
  436.                                         PrintFault(ERROR_REQUIRED_ARG_MISSING,"UpdateIcon");
  437.     
  438.                                         Result = RETURN_ERROR;
  439.                                     }
  440.                                 }
  441.                                 else
  442.                                 {
  443.                                     PrintFault(IoErr(),"UpdateIcon");
  444.     
  445.                                     Result = RETURN_ERROR;
  446.                                 }
  447.     
  448.                                     /* Free argument data. */
  449.     
  450.                                 FreeArgs(ArgsPtr);
  451.                             }
  452.                             else
  453.                                 Printf("UpdateIcon: Could not allocate argument data!\a\n");
  454.  
  455.                             /* Free argument vector data. */
  456.     
  457.                             FreeVec(Args);
  458.                         }
  459.                         else
  460.                             Printf("UpdateIcon: Could not allocate argument data!\a\n");
  461.  
  462.                             /* Free pattern matching data. */
  463.  
  464.                         FreeVec(Anchor);
  465.                     }
  466.                     else
  467.                         Printf("UpdateIcon: Could not allocate AnchorPath!\a\n","icon.library");
  468.  
  469.                         /* Close the remaining libraries... */
  470.  
  471.                     CloseLibrary(UtilityBase);
  472.                 }
  473.                 else
  474.                     Printf("UpdateIcon: Could not open \"%s\"!\a\n","utility.library");
  475.  
  476.                 CloseLibrary(IconBase);
  477.             }
  478.             else
  479.                 Printf("UpdateIcon: Could not open \"%s\"!\a\n","icon.library");
  480.  
  481.             CloseLibrary(DOSBase);
  482.         }
  483.     }
  484.     else
  485.     {
  486.             /* Wait for startup message... */
  487.  
  488.         WaitPort(&ThisProcess -> pr_MsgPort);
  489.  
  490.             /* Disable multitasking. */
  491.  
  492.         Forbid();
  493.  
  494.             /* Reply startup message. */
  495.  
  496.         ReplyMsg(GetMsg(&ThisProcess -> pr_MsgPort));
  497.     }
  498.  
  499.     return(Result);
  500. }
  501.